package cn.edu.zut.hhr.controller;


import cn.edu.zut.hhr.annotation.CacheAnnotation;
import cn.edu.zut.hhr.annotation.LogAnnotation;
import cn.edu.zut.hhr.entity.Article;
import cn.edu.zut.hhr.entity.User;
import cn.edu.zut.hhr.service.ArticleService;
import cn.edu.zut.hhr.utils.UserUtil;
import cn.edu.zut.hhr.vo.RestResponse;
import cn.edu.zut.hhr.vo.params.ArticleParams;
import cn.edu.zut.hhr.vo.params.PageParams;
import com.baomidou.mybatisplus.extension.api.R;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author HHR
 * @since 2021-08-21
 */
@RestController
@RequestMapping("/article")
public class ArticleController {

    //s
    public static final long cacheTimeout = 30;

    @Resource
    private ArticleService articleService;

    /**
     *
     * @param page 当前页
     * @param count 每页几条数据
     * @return
     */
    @GetMapping("/listArchives/{page}/{count}")
    @LogAnnotation(module = "文章",operator = "获取文章列表")
    @CacheAnnotation(time = cacheTimeout)
    public RestResponse getArticleList(
            @PathVariable("page") int page,
            @PathVariable(value = "count") int count
    ){
        if(page <= 0 || count <= 0){
            return RestResponse.fail(400,"页码和文章数目必须大于零");
        }
        return RestResponse.success(articleService.getArticleList(page,count));
    }

    //按照用户喜好获取随机文章, 这里用户id不让前端传递, 根据token获取
    @GetMapping("/randomArchives/{count}")
    public RestResponse getRandomArticle(
            @PathVariable("count") int count
    ){
        User user = UserUtil.getCurrentUser();
        RestResponse result = articleService.getRandomArticle(user,count);
        return result;
    }

    /**
     * 获取最热门的文章
     * @param count 获取数目
     * @return
     */
    @GetMapping("/hot/{count}")
    @LogAnnotation(module = "文章",operator = "获取热门文章列表")
    @CacheAnnotation
    public RestResponse getHotArticleList(@PathVariable("count") int count){
        if(count <= 0){
            return RestResponse.fail(400,"文章数目必须大于零");
        }
        return RestResponse.success(articleService.getHotArticleList(count));
    }

    /**
     * 获取最新文章
     * @param count 获取数目
     * @return
     * 因为主键自增, 所以获取最新文章其实就是获取主键最大的文章, 所以通过主键过滤走聚簇索引能带来更大性能提升
     */
    @GetMapping("/new/{last_idx}/{count}")
    @LogAnnotation(module = "文章",operator = "获取最新文章列表")
    @CacheAnnotation(time = cacheTimeout)
    public RestResponse getNewArticleList(
            @PathVariable("last_idx") int lastIdx,
            @PathVariable("count") int count
    ){
        lastIdx = lastIdx == 0?Integer.MAX_VALUE:lastIdx;
        if(count <= 0){
            return RestResponse.fail(400,"文章数目必须大于等于零");
        }
        return RestResponse.success(articleService.getNewArticleList(lastIdx,count));
    }

    /**
     * 文章归档,返回最近年月的文章数目
     * @return
     */
    @GetMapping("/listArchives")
    @LogAnnotation(module = "文章",operator = "文章归档")
    @CacheAnnotation
    public RestResponse getListArchives(){
        return RestResponse.success(articleService.getListArchives());
    }

    @GetMapping("/id/{id}")
    @LogAnnotation(module = "文章",operator = "获取指定文章")
    @CacheAnnotation
    public RestResponse getArticleById(@PathVariable("id") Integer id){
        return articleService.getArticleById(id);
    }

    @PostMapping("/create")
    @LogAnnotation(module = "文章",operator = "发布文章")
    @CacheAnnotation
    public RestResponse addArticle(@RequestBody ArticleParams article){
        return articleService.addArticle(article);
    }

    @GetMapping("/tid/{tid}")
    @LogAnnotation(module = "文章",operator = "通过标签id获取文章列表")
    @CacheAnnotation
    public RestResponse getArticleListByTid(@PathVariable("tid") Integer tid){
        return articleService.getArticleByTid(tid);
    }

    @GetMapping("/month/{year}/{month}/{page}/{pageSize}")
    @LogAnnotation(module = "文章",operator = "查询指定年月发布的文章")
    @CacheAnnotation
    public RestResponse getArticleListByTime(@PathVariable("year") Integer year,
                                             @PathVariable("month") Integer month,
                                             @PathVariable("page") Integer page,
                                             @PathVariable("pageSize") Integer pageSize){
        return articleService.getArticleListByTime(year,month,page,pageSize);
    }

    @PostMapping("/update/like/{aid}")
    @LogAnnotation(module = "文章",operator = "点赞指定文章")
    @ApiOperation("点赞文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name="aid",value="文章id",dataType = "long"),
            @ApiImplicitParam(name="token",value="value",dataType = "String",paramType = "header")
    })
    public RestResponse likeArticle(@PathVariable("aid") Long aid){
        User user = UserUtil.currentUser();
        return articleService.likeArticle(user.getId(),aid);
    }

    @PostMapping("/update/save/{aid}")
    @ApiOperation("收藏文章")
    @ApiImplicitParams({
            @ApiImplicitParam(name="token",value="value",dataType = "String",paramType = "header"),
            @ApiImplicitParam(name="aid",value="文章id",dataType = "long")
    })
    public RestResponse saveArticle(@PathVariable("aid") Long aid){
        User user = UserUtil.currentUser();
        return articleService.saveArticle(user.getId(),aid);
    }

    @GetMapping("/save/{page}/{page_size}")
    @ApiOperation("获取当前用户收藏的文章")
    @ApiImplicitParams({
        @ApiImplicitParam(name="page",value = "页数, 从0开始",dataType = "int", paramType = "path"),
            @ApiImplicitParam(name="page_size", value="每页大小",dataType = "int",paramType = "path")
    })
    public RestResponse getSaveArticle(
            @PathVariable("page") Integer page,
            @PathVariable("page_size") Integer pageSize
    ){
        User user = UserUtil.currentUser();
        return articleService.getSaveArtByUid(user.getId(),page,pageSize);
    }
}
